package com.example.mynewsweb.controller;

import com.example.mynewsweb.po.Comment;
import com.example.mynewsweb.po.New;
import com.example.mynewsweb.service.CommentService;
import com.example.mynewsweb.service.NewService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Controller
@CrossOrigin
@RestController
public class NewController {
    @Autowired
    private NewService newService;
    @Autowired
    private CommentService commentService;
    @Value("${photo.dir}")
    private String realPath;

    /*发布新闻*/
    @PostMapping("api/addNew")
    public Map<String,Object> addNew(New news, MultipartFile photo, HttpServletRequest request)throws Exception {
        log.info("新闻信息: [{}]", news.toString());
        log.info("图片信息: [{}]", photo.getOriginalFilename());
        Map<String,Object>map=new HashMap<>();
        try{
            //图片保存
            String fileName = photo.getOriginalFilename();
            String newFileName= UUID.randomUUID().toString()+"."+ FilenameUtils.getExtension(photo.getOriginalFilename());//获取文件名后缀
            photo.transferTo(new File(realPath,newFileName));
            //设置图片地址
            news.setPath(newFileName);
            String fileUrl="http://localhost:9090/mynewsweb";
            //文件获取路径
            fileUrl = fileUrl + request.getContextPath() + "/pthotos/" + fileName;
            //设置发布时间
            Date date=new Date();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            news.setTime(dateFormat.format(date));
            //保存新闻信息
            newService.addNew(news);
            map.put("state",true);
            map.put("msg","发布成功");
            map.put("url",fileUrl);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","发布失败");
        }
        return map;
    }

    /*发布新闻2*/
    @PostMapping("api/addNew2")
    public Map<String,Object> addNew2(@RequestBody New news)throws Exception {
        log.info("新闻信息: [{}]", news.toString());
        Map<String,Object>map=new HashMap<>();
        try{
            //设置发布时间
            Date date=new Date();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            news.setTime(dateFormat.format(date));
            //保存新闻信息
            newService.addNew2(news);
            map.put("state",true);
            map.put("msg","发布成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","发布失败");
        }
        return map;
    }
    /*所有新闻*/
    @ResponseBody
    @GetMapping ("api/AllNews")
    public List<New> findALl(){
        return newService.findAll();
    }

    /*根据id查询新闻*/
    @ResponseBody
    @GetMapping("api/findOne")
    public New findOne(String id){
        log.info("查看新闻的id: [{}]",id);
        return newService.findOne(id);
    }

    /*根据id查询新闻(客户端)*/
    @ResponseBody
    @GetMapping("api/findone")
    public List<New> findone(String id){
        log.info("查看新闻的id: [{}]",id);
        return newService.findone(id);
    }

    /*按作者id查询新闻*/
    @ResponseBody
    @GetMapping("api/findNewByuserId")
    public List<New> findByuserId(String userId){
        return newService.findByuserId(userId);
    }

    /*新闻分类展示*/
    @ResponseBody
    @GetMapping("api/findType")
    public List<New> findType(String type){
        log.info("查看新闻的种类: [{}]",type);
        return newService.findType(type);
    }

    /*删除新闻*/
    @PostMapping("api/deleteNew")
    public Map<String,Object> deleteNew(@RequestBody New news){
        log.info("新闻: [{}]",news);
        Map<String,Object>map=new HashMap<>();
        try{
            New newPath=newService.findOne(news.getId());
            File file=new File(realPath,newPath.getPath());
            if(file.exists())file.delete();//删除图片
            newService.deleteNew(news);
            String newId=news.getId();
            commentService.deleteComment(newId);
            map.put("state",true);
            map.put("msg","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","删除失败");
        }
        return map;
    }

}
